package com.uft.model.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.uft.model.entity.ConfigProperty;
import com.uft.model.entity.DPProfile;
import com.uft.model.entity.Profile;

@Repository
public class ConfigPropertyDAO {

	@PersistenceContext private EntityManager em;
	private  static Logger  logger = Logger.getLogger(ConfigPropertyDAO.class);
	
	public List<ConfigProperty> getConfigPropertyListByProfileId(int profileId) {
		// TODO Auto-generated method stub
		logger.info("Get all the list by profileId: "+profileId);
		TypedQuery<ConfigProperty> query = em.createQuery(
		           "SELECT cp FROM ConfigProperty cp where cp.profileId="+profileId, ConfigProperty.class);
		return query.getResultList();	
	}
	
	public List<HashMap<String,String>> getConfigPropertyIdName(int profileId){
		List<HashMap<String,String>> result= new ArrayList<HashMap<String,String>>();
		List<Object[]> rows= em.createQuery(
		           "SELECT cp.id , cp.serverName FROM ConfigProperty cp where cp.profileId="+profileId).getResultList();
		for (Object[] row: rows) {
			HashMap<String,String> temp= new HashMap<String,String>();
			temp.put("id",  String.valueOf(row[0]));
			temp.put("serverName",  String.valueOf(row[1]));
			result.add(temp);
		}
		 return result;
		
	}
	
	public String getConfigFile(String id){
		return getConfigProperty(Integer.parseInt(id)).getConfigFile();
	}
	
	  public ConfigProperty getConfigProperty(int id){
		  ConfigProperty configProp = em.find(ConfigProperty.class, id);
		  return configProp;
	  }
	 @Transactional
	   public void addNewConfigProperty(ConfigProperty configProperty){
		   logger.info("Profile Id: "+configProperty.getProfileId());
		   em.persist(configProperty);	  
		   logger.info(" new Config Property Id: "+configProperty.getId());
	   }
	   
	   @Transactional
	   public void updateConfigProperty(ConfigProperty configProperty){
		   logger.info("Updating Config Property id name: "+configProperty.getId());
		   em.merge(configProperty);
	   }
	   
	   @Transactional
	   public void deleteConfigProperty(ConfigProperty configProperty){
		  
		   logger.info("Deleting Config Property id name: "+configProperty.getId());
		   ConfigProperty configProp = em.find(ConfigProperty.class, configProperty.getId());
		   em.remove(configProp);
	   }
	   
	   @Transactional
	   public void deleteConfigPropertyById(int id){
		  
		   logger.info("Deleting Config Property id name: "+id);
		   ConfigProperty configProp = em.find(ConfigProperty.class, id);
		   em.remove(configProp);
	   }
	   @Transactional
	   public void deleteAllConfigPropertyByProfileId(int profileId){
		  
		   logger.info("Deleting all Config Property by Profile Id: "+profileId);
		   Object rows= em.createQuery(
		           "DELETE FROM ConfigProperty cp where cp.profileId="+profileId);
		  // ConfigProperty configProp = em.find(ConfigProperty.class, id);
		  // em.remove(configProp);
	   }
	   
	   
}
